package me.shurufa.widget.animutils;

import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Create by andy (https://github.com/andyxialm)
 * Create time: 16/5/4 11:40
 * Description : Press动画
 */
public class AnimOnTouchListener implements View.OnTouchListener {

    private static final int DURATION = 150;
    private static final float SCALE = 1.4f;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.animate().scaleX(SCALE).scaleY(SCALE).setDuration(DURATION).setInterpolator(new LinearInterpolator());
                v.setPressed(true);
                break;

            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                float y = event.getY();
                boolean isInside = (x > 0 && x < v.getWidth() && y > 0 && y < v.getHeight());
                if (v.isPressed() != isInside) {
                    v.setPressed(isInside);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                v.setPressed(false);
                v.animate().scaleX(1.0f).scaleY(1.0f).setInterpolator(new LinearInterpolator());
                break;
            case MotionEvent.ACTION_UP:
                v.animate().scaleX(1.0f).scaleY(1.0f).setInterpolator(new LinearInterpolator());
                if (v.isPressed()) {
                    v.performClick();
                    v.setPressed(false);
                }
                break;
        }
        return true;
    }
}
